package com.yangjunli.bishe.controller;


import com.yangjunli.bishe.core.RequestHolder;
import com.yangjunli.bishe.core.ReturnT;
import com.yangjunli.bishe.entity.BbsArticle;
import com.yangjunli.bishe.entity.Comment;
import com.yangjunli.bishe.mapper.BbsArticleMapper;
import com.yangjunli.bishe.mapper.CommentMapper;
import com.yangjunli.bishe.service.IBbsArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-04-05
 */
@RestController
@RequestMapping("/bbs-article")
@Slf4j
public class BbsArticleController {


    @Autowired
    IBbsArticleService iBbsArticleService;

    @Resource
    BbsArticleMapper bbsArticleMapper;

    @Resource
    CommentMapper commentMapper;

    @PostMapping()
    public ReturnT creatBbsArticle(BbsArticle bbsArticle) {
        try {
            iBbsArticleService.save(bbsArticle);
        } catch (Exception e) {
            return new ReturnT(100, "添加成功");
        }
        return new ReturnT(200, "添加成功");
    }


    @RequestMapping(value = "/upload")
    @ResponseBody
    public ReturnT upload(@RequestParam("file") MultipartFile file,
                          @RequestParam("artTitle") String artTitle,
                          @RequestParam("artContent") String artContent) {
        log.info("name: " + artTitle);
        if (file.isEmpty()) {
            return new ReturnT(100, "文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        log.info("上传的文件名为:" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        log.info("上传的后缀名为:" + suffixName);
        fileName = UUID.randomUUID().toString().replaceAll("-", "");
        fileName = fileName + suffixName;
        // 文件上传路径
        String filePath = "d:/roncoo/ttt/";
        // 解决中文问题,liunx 下中文路径,图片显示问题
        //fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);

            BbsArticle bbsArticle = new BbsArticle();

            bbsArticle.setArtUserId(RequestHolder.getAccessToken().getId());
            bbsArticle.setArtTitle(artTitle);
            bbsArticle.setArtTypeId(12);
            bbsArticle.setArtContent(artContent);
            bbsArticle.setArtCreTime(LocalDateTime.now());
            bbsArticle.setArtView(0);
            bbsArticle.setArtComNum(0);
            bbsArticle.setArtLikeNum(0);
            bbsArticle.setImageUrl(fileName);


            bbsArticleMapper.insert(bbsArticle);


            return new ReturnT(200, "添加成功");
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ReturnT(100, "添加成功");
    }

    @GetMapping("/{id}")
    public ReturnT queryBbsArticle(@PathVariable("id") Integer id) {
        BbsArticle bbsArticle = null;
        try {
            bbsArticle = bbsArticleMapper.selectById(id);
            List<Comment> comments = commentMapper.selectByBbsArticleId(id);
            bbsArticle.setComment(comments);
        } catch (Exception e) {
            return new ReturnT(100, "获取失败");
        }
        return new ReturnT(bbsArticle);
    }


    @GetMapping
    public ReturnT queryBbsArticleList(){
        try {

            return new ReturnT( bbsArticleMapper.selectAllBbs());
        }catch (Exception e){
            return new ReturnT(100,"获取失败");
        }
    }


    @GetMapping("/artUserId/{pageSize}/{pageNum}")
    public ReturnT queryBbsArticleArtUserIdList(@PathVariable("pageSize") Integer pageSize ,@PathVariable("pageNum") Integer pageNum ) {
        try {
            Integer id = RequestHolder.getAccessToken().getId();
            Long aLong = bbsArticleMapper.selectAllBbsArtUserIdCount(id);
            Map<String,Object> result = new HashMap<>();
            List<BbsArticle> bbsArticles = bbsArticleMapper.selectAllBbsArtUserId(id, pageSize, pageNum);
            result.put("count",aLong);
            result.put("data",bbsArticles);
            return new ReturnT(result);
        } catch (Exception e) {
            return new ReturnT(100, "获取失败");
        }
    }


    @PostMapping("/del")
    public ReturnT delBbsArticle(@RequestBody  Map<String,Integer> reqMap) {
        try {
            Integer id = reqMap.get("id");
            iBbsArticleService.removeById(id);
        } catch (Exception e) {
            return new ReturnT(100, "删除失败");
        }
        return new ReturnT(200, "删除成功");
    }

    @PutMapping("/like/{id}")
    public ReturnT likeBbsArticle(@PathVariable("id") Integer id) {
        try {
            bbsArticleMapper.updateLikeBbs(id);
            return new ReturnT(200, "点赞成功");
        } catch (Exception e) {
            return new ReturnT(100, "操作失败");
        }
    }


}
